package com.starhub.api.docarchive.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.starhub.api.docarchive.entity.DocArchiveBase;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**   
 * 系统知识库管理DAO层
 * @version: V1.0
 * @author: liuhf
 * @time 2025-05-18
 */
@Mapper
public interface DocArchiveBaseMapper extends BaseMapper<DocArchiveBase> {
    
    @Select("SELECT a.* FROM doc_archive_base a " +
            "INNER JOIN sys_user f ON a.user_id = f.id " +
            "WHERE (f.name like CONCAT('%', #{keyWord}, '%') or a.title like CONCAT('%', #{keyWord}, '%'))  " +
            "AND f.status = 'active' " +
            "AND f.id = #{userId} " +
            "AND a.delete_type = 0 " +
            "ORDER BY a.create_time DESC")
    Page<DocArchiveBase> selectPersonalArchives(Page<DocArchiveBase> page, @Param("keyWord") String keyWord, @Param("userId") Long userId);

    @Select("SELECT a.* FROM doc_archive_base a " +
            "INNER JOIN sys_user f ON a.user_id = f.id " +
            "WHERE (f.name like CONCAT('%', #{keyWord}, '%') or a.title like CONCAT('%', #{keyWord}, '%'))  " +
            "AND f.status = 'active' " +
            "AND a.is_shared = 1 "+
            "AND a.delete_type = 0 " +
            "ORDER BY a.create_time DESC")
    Page<DocArchiveBase> selectSystemArchives(Page<DocArchiveBase> page, @Param("keyWord") String keyWord);

    @Select("SELECT a.* FROM doc_archive_base a " +
            "INNER JOIN sys_user f ON a.user_id = f.id " +
            "INNER JOIN doc_archive_follow d ON a.id = d.archive_id " +
            "WHERE (f.name like CONCAT('%', #{keyWord}, '%') or a.title like CONCAT('%', #{keyWord}, '%'))  " +
            "AND f.status = 'active' " +
            "AND d.delete_type = 0  " +
            "AND a.delete_type = 0 " +
            "ORDER BY a.create_time DESC")
    Page<DocArchiveBase> selectFollowedArchives(Page<DocArchiveBase> page, @Param("keyWord") String keyWord);
} 